<template>
  <el-dialog
    :title="titleMap[mode]"
    v-model="visible"
    :width="700"
    destroy-on-close
    append-to-body
    @closed="emits('closed')"
  >
    <el-form
      :model="form"
      :rules="rules"
      ref="dialogForm"
      label-width="80px"
      :style="'el-dialog' === 'el-drawer' ? 'padding:0 20px' : ''"
    >
      
      <el-form-item label="车牌号" prop="chepaihao">
        <el-input v-model="form.chepaihao" clearable placeholder="请输入车牌号" />
      </el-form-item>

      <el-form-item label="车型" prop="chexing">
        <el-input v-model="form.chexing" clearable placeholder="请输入车型" />
      </el-form-item>

      <el-form-item label="颜色" prop="color">
        <el-color-picker v-model="form.color" :predefine="proxy.$CONFIG.COLOR_LIST" />
      </el-form-item>

      <el-form-item label="最大体积" prop="zuidatiji">
        <el-input v-model="form.zuidatiji" clearable placeholder="请输入最大体积" />
      </el-form-item>

      <el-form-item label="载重" prop="zaizhong">
        <el-input v-model="form.zaizhong" clearable placeholder="请输入载重" />
      </el-form-item>

      <el-form-item label="载人数" prop="zairen">
        <el-input v-model="form.zairen" clearable placeholder="请输入载人数" />
      </el-form-item>

      <el-form-item label="准假驾照" prop="jiazhao">
        <el-input v-model="form.jiazhao" clearable placeholder="请输入准假驾照" />
      </el-form-item>

      <el-form-item label="备注" prop="beizhu">
        <el-input v-model="form.beizhu" clearable placeholder="请输入备注" />
      </el-form-item>

      <el-form-item label="默认司机" prop="username">
        <el-input v-model="form.username" clearable placeholder="请输入默认司机" />
      </el-form-item>

      <el-form-item label="gps" prop="gpsid">
        <el-input v-model="form.gpsid" clearable placeholder="请输入gps" />
      </el-form-item>

      <el-form-item label="" prop="quyu">
        <el-input v-model="form.quyu" clearable placeholder="请输入" />
      </el-form-item>

      <el-form-item label="状态" prop="status">
        <el-switch v-model="form.status" :active-value="proxy.$CONFIG.SWITCH.ENABLE" :inactive-value="proxy.$CONFIG.SWITCH.DISABLE" />
      </el-form-item>

      <el-form-item label="排序" prop="sort">
        <el-input v-model="form.sort" clearable placeholder="请输入排序" />
      </el-form-item>

    </el-form>
    <template #footer>
      <el-button @click="visible = false">取 消</el-button>
      <el-button type="primary" :loading="isSaveing" @click="submit()">保 存</el-button>
    </template>
  </el-dialog>
</template>

<script setup>
  import { ref, reactive, defineExpose, onMounted,getCurrentInstance } from 'vue'
  import {useRouter} from "vue-router";
  import { ElMessage } from 'element-plus'
  import wmsTmsWmsMdCheliang from '@/api/apis/wms/wmsTmsWmsMdCheliang'
  import systemDict from '@/api/apis/system/dataDict'


  const emits = defineEmits(['success', 'closed'])
  const { proxy } = getCurrentInstance();
  const router = useRouter();

  const mode = ref('add')
  const treeList = ref([])
  const visible = ref(false)
  const isSaveing = ref(false)
  const dialogForm = ref(null)

  const titleMap = reactive({ add: '新增车辆', edit: '编辑车辆' })
  const dictData = reactive({
    platform_data_status: [],
    
  })
  const form = reactive({
    id:'',
chepaihao:'',
chexing:'',
color:proxy.$TOOL.data.get('APP_COLOR') || proxy.$CONFIG.COLOR || '#536dfe',
zuidatiji:'',
zaizhong:'',
zairen:'4',
jiazhao:'',
beizhu:'',
username:'',
gpsid:'',
quyu:'',
status:proxy.$CONFIG.SWITCH.ENABLE,
sort:'0',
mer_id:'0',

  })
  const rules = reactive({
    chepaihao: [{required: true, message: '车牌号必填', trigger: 'blur' }],
    color: [{required: true, message: '颜色必填', trigger: 'blur' }],
    
  })

  onMounted(async () => {
    await getDictData()
    if(router.currentRoute.value.query.open) {
    	open('add');
    }
  })

  const getDictData = () => {
    systemDict.getDict('platform_data_status').then(res => {
      dictData.platform_data_status = res.data
    })
    
  }

  const open = (type = 'add') => {
    mode.value = type
    visible.value = true
    
  }

  const submit = () => {
    dialogForm.value.validate(async (valid) => {
      if (valid) {
        isSaveing.value = true
        const res = mode.value === 'add' ? await wmsTmsWmsMdCheliang.save(form) : await wmsTmsWmsMdCheliang.update(form.id, form)
        if (res.success) {
          emits('success', form, mode.value)
          visible.value = false
          ElMessage.success(res.message)
        } else {
          ElMessage.error(res.message)
        }
        isSaveing.value = false
      }
    })
  }

  const setData = (data) => {
    for (let k in form) {
      if (data[k] || data[k] === 0) {
        form[k] = data[k]
      }
    }
  }


  defineExpose({
    open, setData
  })
</script>
